package com.android.tiango.baba.view;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Build;
import android.view.View;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.adapter.ProvinceWheelAdapter;
import com.android.tiango.baba.db.Area;
import com.android.tiango.baba.db.AssetsDatabaseManager;
import com.android.tiango.baba.db.City;
import com.android.tiango.baba.db.DbHelper;
import com.android.tiango.baba.db.Province;
import com.android.tiango.baba.interfaces.OnWheelProvinceChangedListener;
import com.android.tiango.baba.util.StringUtils;
import com.j256.ormlite.dao.Dao;

public class WheelProvinceMain {

	private View view;
	private WheelProvinceView wv_province;
	private WheelProvinceView wv_city;
	private WheelProvinceView wv_county;
	private List<String> mProvince_s = new ArrayList<String>();
	private List<String> mCity_s = new ArrayList<String>();
	private List<String> mArea_s = new ArrayList<String>();
	public int screenheight;
	private boolean hasSelectTime;
	private Context mContext;
	private String mProvinceName;
	private String mCityName;
	private String mAreaName;
	private List<Province> mProviceData;
	private List<Area> mAreaData;
	private List<City> mCityData;
	public DbHelper helper;
	private int mProvinceId;
	private int mAreaId;
	private int mCityId;

	public View getView() {
		return view;
	}

	public void setView(View view) {
		this.view = view;
	}

	public WheelProvinceMain(View view, Context context) {
		super();

		this.view = view;
		this.mContext = context;
		hasSelectTime = false;
		AssetsDatabaseManager.initManager(context.getApplicationContext());
		AssetsDatabaseManager mg = AssetsDatabaseManager.getManager();
		mg.getDatabase("ormlite.db");
		helper = new DbHelper(context);
		setView(view);

	}

	public WheelProvinceMain(View view, boolean hasSelectTime) {
		super();
		this.view = view;
		this.hasSelectTime = hasSelectTime;
		setView(view);
	}

	public void initProvincePicker(int province, int city, int county) {
		this.initDateTimePicker(province, city, county);
	}

	/**
	 * @Description: TODO 弹出日期时间选择器
	 */
	public void initDateTimePicker(int provinces, int citys, int countys) {
		// 省
		wv_province = (WheelProvinceView) view.findViewById(R.id.province);

		try {
			Dao<Province, Integer> dataPro = helper.getProvinceDao();
			mProviceData = dataPro.queryForAll();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mProvince_s.clear();
		for (int i = 0; i < mProviceData.size(); i++) {
			mProvince_s.add(mProviceData.get(i).getProvinceName());
		}
		wv_province.setAdapter(new ProvinceWheelAdapter(mProvince_s, mContext));// 设置"年"的显示数据
		wv_province.setCyclic(true);// 可循环滚动
		wv_province.setCurrentItem(provinces);// 初始化时显示的数据
		mProvinceName = mProviceData.get(0).getProvinceName();
		mProvinceId = mProviceData.get(0).getProvinceId();

		// 市
		wv_city = (WheelProvinceView) view.findViewById(R.id.city);
		Dao<City, Integer> daoCityD;
		try {
			daoCityD = helper.getCityDao();
			mCityData = daoCityD.queryForEq("provinceId", mProviceData.get(0).getProvinceId());
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		mCity_s.clear();
		for (int i = 0; i < mCityData.size(); i++) {
			mCity_s.add(mCityData.get(i).getCityName());
		}

		wv_city.setAdapter(new ProvinceWheelAdapter(mCity_s, mContext));
		if (mCity_s.size() < 4) {
			wv_city.setCyclic(false);
		} else {
			wv_city.setCyclic(true);
		}
		wv_city.setCurrentItem(citys);
		mCityName = mCityData.get(0).getCityName();
		mCityId = mCityData.get(0).getCityId();

		// 区
		wv_county = (WheelProvinceView) view.findViewById(R.id.county);
		Dao<Area, Integer> daoAreaD;
		try {
			daoAreaD = helper.getAreaDao();
			mAreaData = daoAreaD.queryForEq("cityId", mCityData.get(0).getCityId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mArea_s.clear();
		for (int i = 0; i < mAreaData.size(); i++) {
			mArea_s.add(mAreaData.get(i).getAreaName());
		}
		wv_county.setAdapter(new ProvinceWheelAdapter(mArea_s, mContext));
		if (mArea_s.size() < 4) {
			wv_county.setCyclic(false);
		} else {
			wv_county.setCyclic(true);
		}
		wv_county.setCurrentItem(countys);
		mAreaName = mAreaData.get(0).getAreaName();
		mAreaId = mAreaData.get(0).getAreaId();
		OnWheelProvinceChangedListener wheelListener_province = new OnWheelProvinceChangedListener() {
			@Override
			public void onChanged(WheelProvinceView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub

				if (newValue != oldValue) {

					Dao<City, Integer> daoCityD;
					try {
						daoCityD = helper.getCityDao();
						mCityData = daoCityD.queryForEq("provinceId", mProviceData.get(newValue)
								.getProvinceId());
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					mProvinceId = mProviceData.get(newValue).getProvinceId();
					mProvinceName = mProviceData.get(newValue).getProvinceName();
					mCity_s.clear();
					for (int i = 0; i < mCityData.size(); i++) {
						mCity_s.add(mCityData.get(i).getCityName());
					}

					wv_city.setAdapter(new ProvinceWheelAdapter(mCity_s, mContext));
					if (mCity_s.size() < 4) {
						wv_city.setCyclic(false);
					} else {
						wv_city.setCyclic(true);
					}
					wv_city.setCurrentItem(0);
					mCityName = mCityData.get(0).getCityName();
					mCityId = mCityData.get(0).getCityId();

					Dao<Area, Integer> daoAreaD;
					try {
						daoAreaD = helper.getAreaDao();
						mAreaData = daoAreaD.queryForEq("cityId", mCityData.get(0).getCityId());
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					mArea_s.clear();
					for (int i = 0; i < mAreaData.size(); i++) {
						mArea_s.add(mAreaData.get(i).getAreaName());
					}
					wv_county.setAdapter(new ProvinceWheelAdapter(mArea_s, mContext));
					if (mArea_s.size() < 4) {
						wv_county.setCyclic(false);
					} else {
						wv_county.setCyclic(true);
					}
					if (mArea_s.size() > 0 && mAreaData.size() != 0) {
						wv_county.setCurrentItem(0);
						mAreaName = mAreaData.get(0).getAreaName();
						mAreaId = mAreaData.get(0).getAreaId();
					} else {
						mAreaName = "";
						mAreaId = 0;
					}

				}
			}
		};
		OnWheelProvinceChangedListener wheelListener_city = new OnWheelProvinceChangedListener() {

			@Override
			public void onChanged(WheelProvinceView wheel, int oldValue, int newValue) {
				if (newValue != oldValue) {
					Dao<Area, Integer> daoAreaD;
					try {
						daoAreaD = helper.getAreaDao();
						mAreaData = daoAreaD.queryForEq("cityId", mCityData.get(newValue)
								.getCityId());
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					mCityId = mCityData.get(newValue).getCityId();
					mCityName = mCityData.get(newValue).getCityName();
					mArea_s.clear();
					for (int i = 0; i < mAreaData.size(); i++) {
						mArea_s.add(mAreaData.get(i).getAreaName());
					}
					wv_county.setAdapter(new ProvinceWheelAdapter(mArea_s, mContext));
					if (mArea_s.size() < 4) {
						wv_county.setCyclic(false);
					} else {
						wv_county.setCyclic(true);
					}
					if (mArea_s.size() > 0 && mAreaData.size() != 0) {
						wv_county.setCurrentItem(0);
						mAreaName = mAreaData.get(0).getAreaName();
						mAreaId = mAreaData.get(0).getAreaId();
					} else {
						mAreaName = "";
						mAreaId = 0;
					}
				}
			}
		};
		OnWheelProvinceChangedListener wheelListener_area = new OnWheelProvinceChangedListener() {

			@Override
			public void onChanged(WheelProvinceView wheel, int oldValue, int newValue) {
				if (newValue != oldValue) {
					if (mArea_s.size() > 0 && mAreaData.size() != 0) {
						mAreaName = mAreaData.get(newValue).getAreaName();
						mAreaId = mAreaData.get(newValue).getAreaId();
					} else {
						mAreaName = "";
						mAreaId = 0;
					}
				}
			}
		};
		wv_province.addChangingListener(wheelListener_province);
		wv_city.addChangingListener(wheelListener_city);
		wv_county.addChangingListener(wheelListener_area);

		// 根据屏幕密度来指定选择器字体的大小(不同屏幕可能不同)
		int textSize = 0;
		// if (hasSelectTime)
		// textSize = (screenheight / 100) * 3;
		// else
		// textSize = (screenheight / 100) * 2;
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
			textSize = (screenheight / 100) * 3;
		} else {
			textSize = (screenheight / 100) * 2;
		}
		wv_city.TEXT_SIZE = textSize;
		wv_province.TEXT_SIZE = textSize;
		wv_county.TEXT_SIZE = textSize;
		wv_city.invalidate();
		wv_county.invalidate();
		wv_province.invalidate();

	}

	public String getAddress() {
		String string = "";
		if (!StringUtils.isEmpty(mProvinceName)) {
			string = string + mProvinceName;
		}
		if (!StringUtils.isEmpty(mCityName)) {
			string = string + mCityName;
		}
		if (!StringUtils.isEmpty(mAreaName)) {
			string = string + mAreaName;
		}

		return string;
	}

	public int[] getID() {
		int[] id = new int[3];

		if (mProvinceId != 0) {
			id[0] = mProvinceId;
		}
		if (mCityId != 0) {
			id[1] = mCityId;
		}
		if (mAreaId != 0) {
			id[2] = mAreaId;
		}

		return id;
	}
}
